package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.DaoSchoolArticle;
import com.sc.nft.entity.dto.DaoSchoolArticleNotChildIdPageListDTO;
import com.sc.nft.entity.dto.DaoSchoolArticleTopDTO;
import com.sc.nft.entity.dto.DaoSchoolCancelArticleTopDTO;
import com.sc.nft.entity.vo.ArticleDetailsVO;
import com.sc.nft.entity.vo.DaoSchoolArticlePageAdminVO;
import com.sc.nft.entity.vo.DaoSchoolTopHomeArticleListVO;
import com.sc.nft.mapper.DaoSchoolArticleMapper;
import com.sc.nft.sup.PageRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 文章表(DaoSchoolArticle)表Dao层
 *
 * @author makejava
 * @since 2023-03-13 16:20:42
 */
@Slf4j
@Repository
public class DaoSchoolArticleDao extends ServiceImpl<DaoSchoolArticleMapper, DaoSchoolArticle> {

    /**
     * 后台获取分页列表
     *
     * @param pageRequest
     * @return
     */
    public Page<DaoSchoolArticlePageAdminVO> pageAdmin(PageRequest pageRequest) {
        return baseMapper.pageAdmin(pageRequest.toMp());
    }

    /**
     * 根据唯一标识获取数据
     *
     * @param id
     * @return
     */
    public DaoSchoolArticle selectById(Long id) {
        return baseMapper.selectById(id);
    }

    /**
     * 获取没有被某个子集选中的文章
     *
     * @param daoSchoolArticleNotChildIdPageListDTO
     * @return
     */
    public Page<DaoSchoolArticlePageAdminVO> selectByNotChildIdPageList(DaoSchoolArticleNotChildIdPageListDTO daoSchoolArticleNotChildIdPageListDTO) {
        return baseMapper.selectByNotChildIdPageList(daoSchoolArticleNotChildIdPageListDTO.toMp(), daoSchoolArticleNotChildIdPageListDTO);
    }

    /**
     * 获取首页推荐文章
     *
     * @param daoSchoolArticleNotChildIdPageListDTO
     * @return
     */
    public Page<DaoSchoolArticlePageAdminVO> homePageAdmin(DaoSchoolArticleNotChildIdPageListDTO daoSchoolArticleNotChildIdPageListDTO) {
        return baseMapper.homePageAdmin(daoSchoolArticleNotChildIdPageListDTO.toMp(), daoSchoolArticleNotChildIdPageListDTO);
    }

    /**
     * 获取没有被置顶的文章
     *
     * @param daoSchoolArticleNotChildIdPageListDTO
     * @return
     */
    public Page<DaoSchoolArticlePageAdminVO> selectByNotTopArticle(DaoSchoolArticleNotChildIdPageListDTO daoSchoolArticleNotChildIdPageListDTO) {
        return baseMapper.selectByNotTopArticle(daoSchoolArticleNotChildIdPageListDTO.toMp(), daoSchoolArticleNotChildIdPageListDTO);
    }

    public Boolean articleTop(List<DaoSchoolArticleTopDTO> list) {
        list.forEach(daoSchoolArticleTopDTO -> {
            DaoSchoolArticle daoSchoolArticle = new DaoSchoolArticle();
            daoSchoolArticle.setId(daoSchoolArticleTopDTO.getId());
            daoSchoolArticle.setHomeSort(daoSchoolArticleTopDTO.getSort());
            daoSchoolArticle.setStatus(1);
            daoSchoolArticle.updateById();
        });
        return Boolean.TRUE;
    }

    public Boolean cancelArticleTop(List<DaoSchoolCancelArticleTopDTO> list) {
        list.forEach(daoSchoolArticleTopDTO -> {
            DaoSchoolArticle daoSchoolArticle = new DaoSchoolArticle();
            daoSchoolArticle.setId(daoSchoolArticleTopDTO.getId());
            daoSchoolArticle.setStatus(0);
            daoSchoolArticle.updateById();
        });
        return Boolean.TRUE;
    }

    /**
     * 获取当前置顶文章数量
     *
     * @return
     */
    public Integer articleTopCount() {
        QueryWrapper<DaoSchoolArticle> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        return baseMapper.selectCount(queryWrapper);
    }

    public Integer articleTopIsExist(List<Long> info) {
        QueryWrapper<DaoSchoolArticle> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        queryWrapper.in("id", info);
        return baseMapper.selectCount(queryWrapper);
    }

    /**
     * 获取首页文章
     *
     * @return
     */
    public List<DaoSchoolTopHomeArticleListVO> getHomeTopArticle() {
        return baseMapper.getHomeTopArticle(6L);
    }

    /**
     * 通过id获取文章
     *
     * @param articleId
     * @return
     */
    public ArticleDetailsVO byIdGetArticle(Long articleId) {
        return baseMapper.byIdGetArticle(articleId);
    }

    /**
     * 通过id增加文章浏览量
     *
     * @param id
     * @param browse
     * @return
     */
    public Boolean increaseArticleViewsBasedOnId(Long id, Integer browse) {
        LambdaUpdateWrapper<DaoSchoolArticle> wrapper = new LambdaUpdateWrapper<>();
        wrapper.setSql("views_number=views_number+" + browse);
        wrapper.eq(DaoSchoolArticle::getId, id);
        baseMapper.update(new DaoSchoolArticle(), wrapper);
        return Boolean.TRUE;
    }

}
